#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <cstdlib>
#include <iostream>
#include <list>
#include <mutex>
#include <thread>

#include "flvserver.h"

int main(int argc, char* argv[]) {
  int port = 8000;  // Default port.
  std::string rtspurl("rtsp://192.168.5.10/stream1");
  int opt;

  while ((opt = getopt(argc, argv, "p:i:")) != -1) {
    switch (opt) {
      case 'p':
        port = std::atoi(optarg);
        break;
      case 'i':
        rtspurl = optarg;
        break;
      default: /* '?' */
        std::cerr << "Usage: " << argv[0]
                  << " [-p port]  [-i rtsp://ip:554/stream]" << std::endl;
        exit(EXIT_FAILURE);
    }
  }

  FlvStreamServer* pFlvServer = new FlvStreamServer(port,rtspurl);
  pFlvServer->run();
  while (1) {
    usleep(3000 * 1000);
  }
  return 0;
}
